package com.itboye.shop.responsitory;

import com.google.gson.reflect.TypeToken;
import com.itboye.shop.bean.DingDanQuery;
import com.itboye.shop.bean.GoodsDetailBean;
import com.itboye.shop.bean.GoodsModel;
import com.itboye.shop.bean.OrderBean;
import com.itboye.shop.bean.ShopCarBean;
import com.itboye.shop.bean.ZiLeiMuBean;
import com.itboye.shop.interfaces.IShop;
import com.itboye.shop.net.BaseErrorListener;
import com.itboye.shop.net.BaseNetRepository;
import com.itboye.shop.net.BaseSuccessReqListener;
import com.itboye.shop.net.ByJsonRequest;
import com.itboye.shop.net.ICompleteListener;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 商品
 */
public class ShopResponsitory extends BaseNetRepository implements IShop<GoodsModel> {

    final String APIKEY = "100";
    final String GoodsSearch = "By_Product_search";//商品列表
    final String mallSousuo = "By_Product_searchKeywords";//商品搜索
    final String payFangshi = "By_Config_supportPayways";//支付方式
    final String mallxq = "By_Product_detail";//商品详情
    final String leimu = "By_Category_querySubCategory";//子类目查询
    final String gouwuche = "By_ShoppingCart_query";//查询购物车
    final String deletegouwuche = "By_ShoppingCart_delete";//删除购物车
    final String updategouwuche = "By_ShoppingCart_update";//删除购物车
    final String chuangjian = "By_Order_create";//订单创建
    final String chaxun = "By_Order_query";//订单分页查询
    final String payByWa="By_Wallet_pay";//余额支付

    public ShopResponsitory(ICompleteListener iCompleteListener) {
        super(iCompleteListener);
    }

    /**
     * 商品列表
     *
     * @param page_index 当前页码[可选,默认1]
     * @param page_size  每页大小[可选,默认10]
     * @param cate_id    类目id[可选]
     * @param prop_id    一级类目属性id[可选]
     * @param order      排序(d=>'默认',pd=>价格从高到低,pa=>价格从低到高)可选
     * @param keyword    关键词[可选]
     * @param l_price    下限价格[可选]
     * @param r_price    上限价格[可选]
     */
    @Override
    public void shangPinList(String page_index, String page_size, String cate_id, String prop_id, String order, String keyword, String l_price, String r_price) {
        Type type = new TypeToken<GoodsModel>() {
        }.getType();
        String appkey = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("page_index", page_index);
        map.put("page_size", page_size);
        map.put("cate_id", cate_id);
        map.put("prop_id", prop_id);
        map.put("order", order);
        map.put("keyword", keyword);
        map.put("l_price", l_price);
        map.put("r_price", r_price);

        (new ByJsonRequest.Builder<GoodsModel>())
                .setTypeVerParamsAndReturnClass(GoodsSearch, appkey, map, type)
                .requestListener(
                        new BaseSuccessReqListener<GoodsModel>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 商品搜索
     *
     * @param keyword 关键词
     */
    @Override
    public void getMallSouSuo(String keyword) {
//        Type type = new TypeToken<List<SouSuoMall>>() {
//        }.getType();
//        Map<String, Object> map = new HashMap<>();
//        map.put("keyword", keyword);
//        (new ByJsonRequest.Builder<List<SouSuoMall>>())
//                .setTypeVerParamsAndReturnClass(mallSousuo, APIKEY, map, type)
//                .requestListener(
//                        new BaseSuccessReqListener<List<SouSuoMall>>(
//                                getListener()))
//                .errorListener(new BaseErrorListener(getListener()))
//                .desEncodeThenBuildAndSend();
    }

    @Override
    public void payFangShi() {
//        Type type = new TypeToken<List<PayFangShiBean>>() {
//        }.getType();
//        Map<String, Object> map = new HashMap<>();
//        (new ByJsonRequest.Builder<List<PayFangShiBean>>())
//                .setTypeVerParamsAndReturnClass(payFangshi, APIKEY, map, type)
//                .requestListener(
//                        new BaseSuccessReqListener<List<PayFangShiBean>>(
//                                getListener()))
//                .errorListener(new BaseErrorListener(getListener()))
//                .desEncodeThenBuildAndSend();
    }

    /**
     * 商品详情
     *
     * @param id
     */
    @Override
    public void mallXiangQing(String id) {
        Type type = new TypeToken<GoodsDetailBean>() {
        }.getType();
        String apk = "102";
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        (new ByJsonRequest.Builder<GoodsDetailBean>())
                .setTypeVerParamsAndReturnClass(mallxq, apk, map, type)
                .requestListener(
                        new BaseSuccessReqListener<GoodsDetailBean>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 子目录查询
     *
     * @param cate_id 类目id
     */
    @Override
    public void LeiMuQuery(String cate_id) {
        Type type = new TypeToken<List<ZiLeiMuBean>>() {
        }.getType();
        String apk = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("cate_id", cate_id);
        (new ByJsonRequest.Builder<List<ZiLeiMuBean>>())
                .setTypeVerParamsAndReturnClass(leimu, apk, map, type)
                .requestListener(
                        new BaseSuccessReqListener<List<ZiLeiMuBean>>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void gouWuChe(String sid, String uid) {
        Type type = new TypeToken<ArrayList<ShopCarBean>>() {
        }.getType();
        String apiVer = "103";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", sid);
        map.put("uid", uid);

        (new ByJsonRequest.Builder<ArrayList<ShopCarBean>>())
                .setTypeVerParamsAndReturnClass(gouwuche, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<ArrayList<ShopCarBean>>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void deleteShop(ShopCarBean shopCarBean) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", shopCarBean.getUid());
        map.put("id", shopCarBean.getId());
        map.put("s_id", "itboye");
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(deletegouwuche, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void updateShop(ShopCarBean shopCarBean) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", shopCarBean.getUid());
        map.put("id", shopCarBean.getId());
        map.put("count", shopCarBean.getCount());
        map.put("s_id", "itboye");
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(updategouwuche, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void generateOrder(String s_id, String uid, String ids, String address_id, String del, String note) {
        Type type = new TypeToken<OrderBean>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", s_id);
        map.put("uid", uid);
        map.put("ids", ids);
        map.put("address_id", address_id);
        map.put("del", del);
        map.put("note", note);

        (new ByJsonRequest.Builder<OrderBean>())
                .setTypeVerParamsAndReturnClass(chuangjian, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<OrderBean>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void orderQuery(String s_id, String uid, String keyword, String query_status, String page_index, String page_size) {
        Type type = new TypeToken<DingDanQuery>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", s_id);
        map.put("uid", uid);
        map.put("keyword", keyword);
        map.put("query_status", query_status);
        map.put("page_index", page_index);
        map.put("page_size", page_size);

        (new ByJsonRequest.Builder<DingDanQuery>())
                .setTypeVerParamsAndReturnClass(chaxun, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<DingDanQuery>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void payByWallet(String uid, String payCode) {

        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("pay_code", payCode);

        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(payByWa, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }


}
